


import { SDKTools } from "../../Init/Managers/TaskSDK/SDKTools";
import { HttpClient } from "../工具/HttpClient";
import PlatformAdapter from "../工具/platforms/BaseAdapter";
import CrossPlatformAdapter from "../工具/platforms/CrossPlatformAdapter";
import { Utility } from "../工具/Utility";

type BindInfo = {
    openid?: string,
    game_appid?: string,
    username: string,
    remark: string
}

export default class BindUserManager {

    private static adapter: PlatformAdapter = CrossPlatformAdapter.adapter; // 适配器实例，用于调用平台相关的方法，如显示弹窗、提示等


    public static async bind(opt: BindInfo) {
        let { username, remark, openid, game_appid } = opt;
        if (!username) {
            this.adapter.showAlert("请输入账户")
            return Promise.resolve(false);
        }
        if (Utility.Checker.checkPhoneNum(username) == false) {
            this.adapter.showAlert("账户格式不正确")
            return Promise.resolve(false);
        }
        game_appid = SDKTools.appid;
        openid = SDKTools.GetData("openID");
        let url = `https://tcb-ym38faz8cx32a27-6cede13bfeb0.service.tcloudbase.com/router/client/openid.pub_bindOpenid`;
        this.adapter.showLoading("正在绑定...");
        let res;
        try {
            res = await HttpClient.get(url, {
                openid, game_appid, username, remark
            });
        }
        catch (e) {
            debugger
            // cc.error("绑定失败", e)
            let msg = e.message || "请求失败，请重试";
            this.adapter.showToast(msg)
            // 绑定失败，可能是网络问题，也可能是服务器问题，需要根据具体情况进行处理
            return Promise.resolve(false);
        }
        finally {
            this.adapter.hideLoading(); 
        }
        if (res.code != 0) {
            // cc.error("绑定失败", res.msg)
            let msg = res.msg || "绑定失败请重试";
            this.adapter.showAlert(msg)
            return Promise.resolve(false);
        }
        // cc.log("绑定成功", res)
        console.log("绑定成功2", res)
        this.adapter.showAlert("绑定成功")
        return Promise.resolve(true);
    }

}